package com.kds.fxtool.controller;

import com.kds.fxtool.service.UpdateService;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

import java.io.IOException;

public class UpdateDialogController {

    @FXML private Label titleLabel;
    @FXML private Label versionLabel;
    @FXML private TextArea releaseNotesArea;
    @FXML private Button updateButton;
    @FXML private Button laterButton;
    @FXML private Button skipButton;

    private UpdateService.VersionInfo versionInfo;
    private Stage stage;
    private boolean updateAccepted = false;

    public static boolean showUpdateDialog(UpdateService.VersionInfo versionInfo) {
        try {
            FXMLLoader loader = new FXMLLoader(
                    UpdateDialogController.class.getResource("/fxml/update.fxml")
            );
            Parent root = loader.load();
            UpdateDialogController controller = loader.getController();
            controller.setVersionInfo(versionInfo);

            Stage stage = new Stage();
            stage.initModality(Modality.APPLICATION_MODAL);
            stage.initStyle(StageStyle.UTILITY);
            stage.setTitle("软件更新");
            stage.setScene(new Scene(root));
            stage.setResizable(false);

            controller.setStage(stage);
            stage.showAndWait();

            return controller.isUpdateAccepted();
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    public void setVersionInfo(UpdateService.VersionInfo versionInfo) {
        this.versionInfo = versionInfo;
        updateUI();
    }

    public void setStage(Stage stage) {
        this.stage = stage;
    }

    public boolean isUpdateAccepted() {
        return updateAccepted;
    }

    private void updateUI() {
        if (versionInfo != null) {
            titleLabel.setText("发现新版本 " + versionInfo.version());
            versionLabel.setText("新版本: " + versionInfo.version());
            releaseNotesArea.setText(versionInfo.releaseNotes());

            if (versionInfo.isMandatory()) {
                skipButton.setVisible(false);
                laterButton.setText("立即更新");
            }
        }
    }

    @FXML
    private void handleUpdate() {
        updateAccepted = true;
        stage.close();
        // 这里可以添加下载和安装逻辑
        showDownloadInfo();
    }

    @FXML
    private void handleLater() {
        if (versionInfo.isMandatory()) {
            handleUpdate();
        } else {
            stage.close();
        }
    }

    @FXML
    private void handleSkip() {
        stage.close();
    }

    private void showDownloadInfo() {
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        alert.setTitle("下载更新");
        alert.setHeaderText("开始下载新版本");
        alert.setContentText("版本 " + versionInfo.version() + " 将开始下载。\n下载完成后请运行安装程序。");
        alert.show();
    }
}